<?php
/**
 * Created by PhpStorm.
 * Admin: Administrator
 * Date: 2018/7/28/028
 * Time: 18:10
 */


namespace App\Http\Controllers\Order;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Service\Order\OrderService;
use Session,Config;
use Cache;
use Excel;

class RechargeOrderController extends Controller
{
    /**
     * @return view
     * 套餐订单列表页面
     */
    public function index()
    {
        return view('Order.recharge_order');
    }


    /**
     * @param Request $request
     * 套餐订单列表
     */
    public function getReOrderList(Request $request)
    {

        $where= $request->except('_token');
        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));
        if($request->has('page'))
        {
            $where['page_no'] = $request->input('page');
            unset($where['page']);
        }
        if($request->has('limit'))
        {
            $where['page_size'] = $request->input('limit');
            unset($where['limit']);
        }else{
            $where['page_size'] = 10;
        }


        foreach($where as $key=>$value)
        {
            if($value =='' || $value == 'undefined')
            {
                unset($where[$key]);
            }
        }

        $Order = new OrderService();
        $data = $Order->getReOrderList($where);

        if($data)
        {
            if(!empty($data['data'])){
                foreach ($data['data'] as $k => &$value)
                {
                    $value['recharge_type_name'] = $value['recharge_type'] == 2?'新用户充值':'老用户续费';
                }
            }
            return $data;
        }



    }


    /**
     * @param Request $request
     * 导出excel
     */
    public function downloadReOrderList(Request $request)
    {


        $where= $request->except('_token');

        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));
        if($request->has('page'))
        {
            $where['page_no'] = $request->input('page');
            unset($where['page']);
        }
        if($request->has('limit'))
        {
            $where['page_size'] = $request->input('limit');
            unset($where['limit']);
        }else{
            $where['page_size'] = 10;
        }


        foreach($where as $key=>$value)
        {
            if($value =='' || $value == 'undefined')
            {
                unset($where[$key]);
            }
        }

        $Order = new OrderService();
        $data = $Order->getReOrderList($where);

        if($data && !empty($data['data']))
        {
            foreach ($data['data'] as $k => &$value)
            {
                $value['recharge_type_name'] = $value['recharge_type'] == 2?'新用户充值':'老用户续费';
                unset($value['order_type']);
                unset($value['order_original']);
                unset($value['pay_channel']);
                unset($value['recharge_type']);
            }

            $cellData = $data['data'];

            $title = ['ID','订单号','充值金额(元)','实际到账金额(元)','手机号','支付类型','创建时间','订单来源','充值类型'];

            array_unshift($cellData,$title);

            Excel::create('充值订单列表',function($excel) use ($cellData){
                $excel->sheet('recharge_order', function($sheet) use ($cellData){
                    $sheet->rows($cellData);
                });
            })->export('xlsx');
        }


    }

}